#!/bin/sh  
#chkconfig: 2345 10 90  
#description: Startup and stop script for Redis  
PATH=/usr/local/redis-4.0.8/bin:/usr/local/bin:/sbin:/usr/bin:/bin  
RPORT1=6380  
RPORT2=6381  
RPORT3=6382  
RPORTS=($RPORT1 $RPORT2 $RPORT3)  
EXEC=/usr/local/redis-4.0.8/bin/redis-server  
REDIS_CLI=/usr/local/redis-4.0.8/bin/redis-cli  
  

RPIDFILE1=/data/redis/${RPORT1}/redis.pid  
RPIDFILE2=/data/redis/${RPORT2}/redis.pid  
RPIDFILE3=/data/redis/${RPORT3}/redis.pid  
RPIDFILES=($RPIDFILE1 $RPIDFILE2 $RPIDFILE3)  

BINDIP=$(/sbin/ifconfig eth1|grep inet|grep -v inet6|awk '{print $2}')

RCONF1=/data/redis/${RPORT1}/conf/redis.conf  
RCONF2=/data/redis/${RPORT2}/conf/redis.conf  
RCONF3=/data/redis/${RPORT3}/conf/redis.conf  
RCONFS=($RCONF1 $RCONF2 $RCONF3)  
case "$1" in  
    start)  
	i=0
        for PID in ${RPIDFILES[@]}  
        do  
                if [ -e $PID ]  
                then  
                        echo "$PID has exist, process is running"  
                else  
                        echo "$PID Start ..."  
                        $EXEC ${RCONFS[i]}  
                fi  
                if [ "$?" == "0" ]  
                then  
                        echo "$PID is running"  
                else  
                        echo "$PID is failed to start"  
                fi  
		let i++
        done  
        ;;  
  
    stop)  
        i=0  
        for PID in ${RPIDFILES[@]}  
        do  
                if [ ! -e $PID ]  
                then  
                        echo "$PID does not exist, process is not running"  
                else  
                        echo "$PID Stoping ..."  
                        $REDIS_CLI -p ${RPORTS[i]} -h $BINDIP  SHUTDOWN  
                        sleep 2  
                        while [ -e $PID ]  
                        do  
                            echo "Waiting for Redis to shutdown ..."  
                            sleep 1  
                        done  
                        echo "$PID stopped"  
                fi  
                let i++  
        done  
        ;;  
  
   restart)  
        ${0} stop  
        ${0} start  
        ;;  
  *)  
    echo "Usage: /etc/init.d/redis {start|stop|restart}" >&2  
        exit 1  
esac 
